//
//  HWTweetImageV.m
//  HomeWork
//
//  Created by apple on 2017/10/25.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HWTweetImageV.h"
#import "HWTweetsModel.h"
#import "SDPhotoBrowser.h"
#import "HWTweetImageViewModel.h"
@interface HWTweetImageV()<SDPhotoBrowserDelegate>

/** imagev数组 */
@property (strong, nonatomic) NSMutableArray *imageViews;
/** 内容控件 */
@property (strong, nonatomic) UIView *contentV;

@end

@implementation HWTweetImageV


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIView *contentV = [[UIView alloc] init];
        [self addSubview:contentV];
        self.contentV = contentV;
        self.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}

- (void)updateConstraints{
    
    // 最大列数
    NSInteger maxCol;
    // 图片宽度
    CGFloat imageWidth;
    if (self.images.count == 4) {
        maxCol = 2;
        imageWidth = homeScale(100);
    } else{
        maxCol = 3;
        imageWidth = homeScale(70);
    }
    
    CGFloat gappingX = homeScale(10);
    CGFloat gappingY = gappingX;
    
    UIView *lastView;
    for (int i = 0; i < self.imageViews.count; i++) {
        
        UIImageView *tweetImgV = self.imageViews[i];
        
        if (i < self.images.count) {
            tweetImgV.hidden = NO;
            if (self.images.count == 1) {
                [tweetImgV mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.top.mas_equalTo(0);
                    make.width.mas_equalTo(homeScale(120));
                    make.height.mas_equalTo(homeScale(210));
                }];
            } else {
                tweetImgV.tag = i;
                NSInteger row = i / maxCol;
                NSInteger col = i % maxCol;
                [tweetImgV mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo((gappingX + imageWidth) * col);
                    make.width.height.mas_equalTo(imageWidth);
                    make.top.mas_equalTo((gappingY + imageWidth) * row);
                }];
            }
            lastView = tweetImgV;
        } else {
            tweetImgV.hidden = YES;
        }
    }
    
    if (self.images.count == 1 || self.images.count == 4) {
        [self.contentV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
            make.height.mas_equalTo(homeScale(210));
        }];
        
        [self mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(homeScale(210));
        }];
        
    } else if (self.images.count){
        NSInteger totalRow = (self.images.count + 2) / 3;
        [self.contentV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
            make.height.mas_equalTo(totalRow * (imageWidth + gappingY) - gappingY);
        }];
        [self mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(totalRow * (imageWidth + gappingY) - gappingY);
        }];
    } else {
        [self.contentV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self);
            make.height.mas_equalTo(0);
        }];
        [self mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
    }
    
    [super updateConstraints];
}

- (NSMutableArray *)imageViews{
    if (!_imageViews) {
        _imageViews = [NSMutableArray array];
    }
    return _imageViews;
}

- (void)setImages:(NSArray *)images{
    
    _images = images;
    while (self.imageViews.count < images.count) {
        
        UIImageView *imageV = [[UIImageView alloc] init];
        imageV.tag = self.imageViews.count;
        imageV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)];
        [imageV addGestureRecognizer:tap];
        imageV.contentMode = UIViewContentModeScaleAspectFill;
        imageV.clipsToBounds = YES;
        [self.contentV addSubview:imageV];
        [self.imageViews addObject:imageV];
    }
    
    for (int i = 0; i < images.count; i++) {
        if (i < self.imageViews.count) {
            HWTweetImageInfo *info = images[i];
            UIImageView *tweetImgV = self.imageViews[i];
            [tweetImgV setImageWithUrl:info.url placeholderImage:PlaceImage];
        }
    }
    
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
}

- (void)imageClick:(UITapGestureRecognizer *)tap{
    UIView *imageView = tap.view;
    SDPhotoBrowser *photoBrowser = [SDPhotoBrowser new];
    photoBrowser.delegate = self;
    photoBrowser.currentImageIndex = imageView.tag;
    photoBrowser.imageCount = self.images.count;
    photoBrowser.sourceImagesContainerView = self.contentV;
    [photoBrowser show];
    
    [self.viewModel.imageClickSubject sendNext:imageView];
    
}

#pragma mark - SDPhotoBrowserDelegate
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    if (index < self.images.count) {
        HWTweetImageInfo *info = self.images[index];
        return [NSURL URLWithString:info.url];
    }
    return nil;
    
}
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    if (index < self.images.count) {
        UIImageView *imageView = self.imageViews[index];
        return imageView.image;
    }
    return nil;
}



@end
